<template>
<div class="address">
  <div class="header">
      <div class="icon" @click="toBack">
        <van-icon name="arrow-left"></van-icon>
      </div>
      <p class="title" style="">收货地址</p>
  </div>
  <van-address-list
  v-model="chosenAddressId"
  :list="list"
  @add="onAdd"
  @edit="onEdit"
  @select="selAddr"
  class="addressList"
  ></van-address-list>
</div>
</template>

<script>
  import { mapMutations,mapGetters } from "vuex";
  import * as types from "../../store/mutation-types";
  import https from '@/common/https.js'
  import commonjs from '@/common/commonUtils.js'

export default {
  name: 'address',
  data () {
    return {
      wechatId: '',
      chosenAddressId: '0',
      list: [
        // {
        //   id: '0',
        //   name: '店铺自取',
        //   tel: '',
        //   address: '前往店铺自取，无需快递费用'
        // },{
        //   id: '1',
        //   name: '张三',
        //   tel: '13000000000',
        //   address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室'
        // }
      ]
    }
  },
  created () {
    console.log("-----------address created----------")
    // 缓存中获取微信id
    let isWchat = commonjs.is_wexin()
    if (isWchat) {
      let wechat = JSON.parse(commonjs.getStorage('wechat'))
      if (wechat !== null) { // 判断是否微信授权登录
        this.wechatId = wechat.wechatId
      }
    } else {
      console.log('this is not wechat browser !!!')
      this.wechatId = '1'
    }
  },
  activated () {
    this.getAddressList(this.wechatId)
    let addrId = this.$route.query.addrId
    console.log("----------address activated---------")
    if (addrId !== undefined) {
      if (addrId === '0') {
        this.chosenAddressId = addrId
      } else {
        this.chosenAddressId = Number.parseInt(addrId)
      }
    }
  },
  computed: {
    ...mapGetters(["userAddress"])
  },
  methods: {
    ...mapMutations({
      setUserAddress : types.SET_USERADDRESS,
    }),
    onAdd () {
      this.$router.push('./addAddress')
    },
    toBack () {
      this.$router.go(-1)
    },
    onEdit (item, index) {
      this.$router.push('/addAddress')
    },
    selAddr (item, index) { //选中 地址
      console.log("select " + item.id)
      let addrId = this.$route.query.addrId
      if (addrId !== undefined) {
        this.setUserAddress(item.id)
        this.$router.go(-1)
      }
    },
    getAddressList (wechatId) {
      let params = {wechatId: wechatId}
      https.fetchPost('/address/webs/findAddressList', params).then((res) => {
        if (res.status === 200) {
          this.list = res.data
        }
      }).catch(err => {
        console.log(err)
      })

    },
  }
}
</script>

<style lang="scss" scoped>
  .address{
    .header{
      height: 46px;
      overflow: hidden;position: absolute;
      width: 100%;
      z-index: 1;
      top: 0;
      background: #fff;
      line-height: 46px;
      border-bottom: 1px solid #f2f2f2;
      .icon{
        position: fixed; left:16px; font-size:14px; color:#1989fa;
      }
      .title{
        margin: 0; padding: 0;
      }
    }
    .addressList{
      margin-top: 50px;
    }
  }
</style>
